Skip to content

Fix fix-orientation command for Imagick and add WP 5.3+ tests#233

Merged
swissspidy merged 8 commits intomainfrom
copilot/review-fix-orientation-command
Mar 11, 2026
Merged

Fix fix-orientation command for Imagick and add WP 5.3+ tests#233
swissspidy merged 8 commits intomainfrom
copilot/review-fix-orientation-command

Conversation

Copy link
Contributor

Copilot AI commented Mar 11, 2026

  • Analyze review feedback requesting @require-extension-imagick test scenarios
  • Add @require-extension-imagick @require-extension-exif @require-wp-4.0 @less-than-wp-5.3 scenario in features/media-fix-orientation.feature testing flip-only orientations (2 and 4) with Imagick
  • Fix PHPStan level 9 type error: use is_scalar() narrowing before absint() for EXIF fallback
  • Fix flip_rotate_image(): only call wp_generate_attachment_metadata() and wp_update_attachment_metadata() after $editor->save() succeeds; return false if generated metadata is empty; remove now-unused $metadata parameter
  • Fix WP 5.3+ false-positive: when DB orientation > 1, cross-check file's current EXIF — if file shows <= 1, the image was already auto-rotated by WP 5.3+ on import; tighten stale-metadata fallback to only trigger when DB has no orientation data at all (not when explicitly 0 after normalization)
  • All checks pass: lint, phpcs, phpstan, code review
Original prompt

This section details on the original issue you should resolve

<issue_title>Review fix-orientation command and tests</issue_title>
<issue_description>The two tests for wp media fix-orientation are currently disabled because they only run on WP < 4.9 but we require 4.9+:

# On WP 4.9 tests this results in "Couldn't fix orientation".
# Todo: Revisit this test and improve or potentially remove it if useless.
@require-extension-exif @require-wp-4.0 @less-than-wp-4.9
Scenario: Fix orientation for all images
Given download:
| path | url |
| {CACHE_DIR}/landscape-2.jpg | https://raw.githubusercontent.com/thrijith/test-images/master/Landscape_2.jpg |
| {CACHE_DIR}/landscape-5.jpg | https://raw.githubusercontent.com/thrijith/test-images/master/Landscape_5.jpg |
| {CACHE_DIR}/portrait-4.jpg | https://raw.githubusercontent.com/thrijith/test-images/master/Portrait_4.jpg |
| {CACHE_DIR}/portrait-6.jpg | https://raw.githubusercontent.com/thrijith/test-images/master/Portrait_6.jpg |
And I run `wp option update uploads_use_yearmonth_folders 0`
When I run `wp media import {CACHE_DIR}/landscape-2.jpg --title="Landscape Two" --porcelain`
Then save STDOUT as {LANDSCAPE_TWO}
When I run `wp media import {CACHE_DIR}/landscape-5.jpg --title="Landscape Five" --porcelain`
Then save STDOUT as {LANDSCAPE_FIVE}
When I run `wp media import {CACHE_DIR}/portrait-4.jpg --title="Portrait Four" --porcelain`
Then save STDOUT as {PORTRAIT_FOUR}
When I run `wp media fix-orientation --dry-run`
Then STDOUT should contain:
"""
3 "Portrait Four" (ID {PORTRAIT_FOUR}) will be affected.
"""
And STDOUT should contain:
"""
3 "Landscape Five" (ID {LANDSCAPE_FIVE}) will be affected.
"""
And STDOUT should contain:
"""
3 "Landscape Two" (ID {LANDSCAPE_TWO}) will be affected.
"""
And STDOUT should contain:
"""
Success: 3 of 3 images will be affected.
"""
When I run `wp media fix-orientation`
Then STDOUT should contain:
"""
3 Fixing orientation for "Portrait Four" (ID {PORTRAIT_FOUR}).
"""
And STDOUT should contain:
"""
3 Fixing orientation for "Landscape Five" (ID {LANDSCAPE_FIVE}).
"""
And STDOUT should contain:
"""
3 Fixing orientation for "Landscape Two" (ID {LANDSCAPE_TWO}).
"""
And STDOUT should contain:
"""
Success: Fixed 3 of 3 images.
"""
# Verify orientation fix.
When I run `wp media fix-orientation`
Then STDOUT should contain:
"""
3 No orientation fix required for "Portrait Four" (ID {PORTRAIT_FOUR}).
"""
And STDOUT should contain:
"""
3 No orientation fix required for "Landscape Five" (ID {LANDSCAPE_FIVE}).
"""
And STDOUT should contain:
"""
3 No orientation fix required for "Landscape Two" (ID {LANDSCAPE_TWO}).
"""
And STDOUT should contain:
"""
Success: Images already fixed.
"""
# On newer versions (5.3+) the image is already considered fixed.
# On WP 4.9 tests this results in "Couldn't fix orientation".
# Todo: Revisit this test and improve or potentially remove it if useless.
@require-extension-exif @require-wp-4.0 @less-than-wp-4.9
Scenario: Fix orientation for single image
Given download:
| path | url |
| {CACHE_DIR}/portrait-6.jpg | https://raw.githubusercontent.com/thrijith/test-images/master/Portrait_6.jpg |
When I run `wp media import {CACHE_DIR}/portrait-6.jpg --title="Portrait Six" --porcelain`
Then save STDOUT as {PORTRAIT_SIX}
When I run `wp media fix-orientation {PORTRAIT_SIX}`
Then STDOUT should be:
"""
1/1 Fixing orientation for "Portrait Six" (ID {PORTRAIT_SIX}).
Success: Fixed 1 of 1 images.
"""
# Verify orientation fix.
When I run `wp media fix-orientation {PORTRAIT_SIX}`
Then STDOUT should be:
"""
1/1 No orientation fix required for "Portrait Six" (ID {PORTRAIT_SIX}).
Success: Image already fixed.
"""
@require-wp-4.0
Scenario: Fix orientation for non existent image
When I try `wp media fix-orientation 9999`
Then STDERR should be:
"""
Error: No images found.
"""

Do we need new tests for 4.9+ or did this command become obsolete and should be deprecated instead?</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…WP 5.3+

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Review fix-orientation command and tests Fix fix-orientation success check and add WP 5.3+ tests Mar 11, 2026
@github-actions github-actions bot added command:media Related to 'media' command command:media-fix-orientation Related to 'media fix-orientation' command scope:testing Related to testing state:unconfirmed labels Mar 11, 2026
@codecov
Copy link

codecov bot commented Mar 11, 2026

Codecov Report

❌ Patch coverage is 18.75000% with 26 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Media_Command.php 18.75% 26 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy

This comment was marked as resolved.

…ion, normalize after save

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Copilot AI changed the title Fix fix-orientation success check and add WP 5.3+ tests Fix fix-orientation command for Imagick and add WP 5.3+ tests Mar 11, 2026
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

@swissspidy swissspidy marked this pull request as ready for review March 11, 2026 10:17
@swissspidy swissspidy requested a review from a team as a code owner March 11, 2026 10:17
Copilot AI review requested due to automatic review settings March 11, 2026 10:17
@swissspidy

This comment was marked as resolved.

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…d fix PHPStan type error

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
gemini-code-assist[bot]

This comment was marked as resolved.

…save, validate non-empty metadata

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

@swissspidy

This comment was marked as resolved.

…to detect already-auto-rotated images

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy merged commit 744b10b into main Mar 11, 2026
59 of 60 checks passed
@swissspidy swissspidy deleted the copilot/review-fix-orientation-command branch March 11, 2026 21:03
@swissspidy swissspidy added this to the 2.2.6 milestone Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:media Related to 'media' command command:media-fix-orientation Related to 'media fix-orientation' command scope:testing Related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review fix-orientation command and tests

3 participants